#ifndef PBVP_TIMER_H
#define PBVP_TIMER_H

#include <time.h>
#ifdef _WIN32
	#include <sys/types.h>
	#include <sys/timeb.h>
#else
	#include <sys/time.h>
#endif

namespace PBVP {
	/// A Class for timing applications
	class Timer
	{
	public:
		/**
		 * Default Constructor
		 */
		Timer() {
			getCurrentTime();
		}

		/// Destructor
		~Timer() {}

		/**
		 * Return current time in seconds
		 * 
		 * @return double
		 */
		inline double getCurrentTime();

		/**
		 * Get time elapsed since last call of timer functions
		 * 
		 * @return double
		 */
		inline double getElapsedTime();
	private:
		double lastTime;
	};

	double Timer::getCurrentTime() 
	{
#ifdef _WIN32
		time_t ltime;
		_timeb tstruct;
		time( &ltime );
		_ftime( &tstruct );
		lastTime = ltime + 1e-3*(tstruct.millitm);
#else
		struct timeval t;
		gettimeofday( &t, NULL );
		lastTime = t.tv_sec + 1e-6*t.tv_usec;
#endif
		return lastTime;
	}

	double Timer::getElapsedTime() 
	{
		double prevTime = lastTime;
		getCurrentTime();
		return (lastTime - prevTime);
	}
}

#endif